// colorui/components/keyboard-verification/Keyboard-verification.js
import {dalogAnimation,dalogContentAnimation,stopDalogAnimation,stopDalogContentAnimation} from "./animation.js"
import {getPhoneCode} from "../../../ajax/module/wallets.js"
Component({
  lifetimes:{
    attached(){
      // this.triggerEvent('getCode', "")
      console.log("嘿嘿",  this.data.openPop)  
      this.setData({
        that:this
      })
      // if(!this.data.openPop){
      //   dalogAnimation(this)
      //   dalogContentAnimation(this)
      // }
      wx.getStorage({
        key: 'loginName',
        success:res=>{
          this.setData({
            realPhone:res.data,
            phone:res.data.substr(0,3) + "****" + res.data.substr(7)
          })
        }
      })

    }
  },
  /**
   * 组件的属性列表
   */
  properties: {
    openPop: {
      type: Boolean,
      // 这里不能使用 箭头函数
      observer: function (news, olds, path) {
       
        // var that =this
        // console.log(that.data.animationData)
        if(news){
          dalogAnimation(this)
          dalogContentAnimation(this)
          setTimeout(()=>{
            this.setData({
              focus:true
            })
          },680)
          
        }else{
          // console.log("9999")
          stopDalogAnimation(this)
          stopDalogContentAnimation(this)
          // this.setData({
          //   animationData:{},
          //   dalogContentAnimation:{}
          // })
        }
      }
      
    }
  },
  // observers:{
    
  // },

  /**
   * 组件的初始数据
   */
  data: {
    animationData:{},
    dalogContentAnimation:{},
    code:"",
    phone:"",
    realPhone:"",
    num:"60",
    flg:true,
    focus:false
  },
  /**
   * 组件的方法列表
   */
  methods: {
    codeListen(e){
      // console.log(e.detail.value)
      this.setData({
        code:e.detail.value
      })
    },
    cancel(){
       this.triggerEvent('cancel', "")
    },
    sure(){
      this.triggerEvent('getCode', this.data.code)
    },
    getCode(){
      if(this.data.flg){
        this.setData({
          focus:false
        })
        getPhoneCode({phone:this.data.realPhone}).then(res=>{
          wx.showToast({
            title: '获取验证码成功',
          })
          this.setData({
            flg:false,
            focus:true
          })
          this.interval(res=>{
            this.setData({
              flg:true
            })
          })
        })  
      }
     

     
    },
    interval(callback){
      //  let time = 60 
      let time=60
      let temp= setInterval(()=>{
        time=time-1
        this.setData({
          num:time
        })
        if(time == 0){
          clearInterval(temp)
          callback()
        }
      },1000)
    }
  }
})
